var through = require('through2');
var Path = require('path');
var fs = require('fs');
var gulp = require('gulp');

function parsePath(path) {
    var extname = Path.extname(path);
    return {
        dirname: Path.dirname(path),
        basename: Path.basename(path, extname),
        extname: extname
    };
}

module.exports = function () {
    return through.obj(function (file, enc, cb) {

        var absPath = parsePath(file.history[0]);

        // �����ļ�Ϊ�գ������κβ�����ת����һ������������һ�� .pipe()
        if (file.isNull()) {
            this.push(file);
            return cb();
        }

        // ������֧�ֶ� Stream ��ֱ�Ӳ������ܳ��쳣
        if (file.isStream()) {
            this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
            return cb();
        }

        // ���ļ�����ת���ַ���
        // Ȼ�󽫴��������ַ���
        try {
            var jpath = file.history[0].replace(".html", ".js")
            console.log(jpath);
            fs.accessSync(jpath)
            file.content.replace("</head>", sprintf("<script src='/dep.js'></script><script src='%s.js'></script></head>", absPath.basename))
        } catch (e) {
            console.log(e)
        }
        console.log(file.content);
        this.push(file);
        cb();
    });
};